package com.zhengke.controller.ddgl;

import com.zhengke.common.BaseController;
import com.zhengke.common.object.AjaxDone;
import com.zhengke.common.object.Result;
import com.zhengke.common.util.DateUtil;
import com.zhengke.common.util.FileExport;
import com.zhengke.pojo.*;
import com.zhengke.service.OrderService;
import com.zhengke.service.coolxinService.JfdhService;
import com.zhengke.service.ddgl.DDGL060Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;


/**  
********************************
* 创建时间：2019年12月27日
* 项目名称：tvmallbs Maven Webapp  
* @author 王鑫
* @version 1.0     
* 文件名称：DDGL060Controller.java  
* 类说明：  

********************************
*/
@Controller
@RequestMapping("DDGL060")
public class DDGL060Controller extends BaseController {
	
	@Autowired
	private DDGL060Service service;
	@Autowired
	private OrderService orderService;
	@Autowired
	private JfdhService jfdhService;


	/**
	 * 跳转可退货列表页
	 * @return
	 */
	@RequestMapping("query.do")
	public String query() {
		return "order/ddgl060/ddgl0601";
	}

	/**
	 * 可退货列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("list.do")
	public Result list(String orderSn,String userName, String userCardNumber,String receiverPhone,  String orderState, int page, int limit){

		Result rs = null;
		try {
			rs = service.queryOrder(orderSn, userName,userCardNumber,receiverPhone,  orderState,page,limit);
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Result();
	}

	/**
	 * 跳转退单详情页
	 * @return
	 */
	@RequestMapping("toReturnDetail.do")
	public String toReturnDetail(){
		return "order/ddgl060/ddglo602";
	}

	/**
	 * 返回退单详情数据
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("returnDetail.do")
	public Result returnDetail(HttpServletRequest request){
		String orderId = request.getParameter("orderId");
		Result rs = null;
		try {
			rs = service.rtOrderDetail(orderId);
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Result();
	}

	/**
	 * 退货
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("toReturn.do")
	public AjaxDone returnGoods(HttpServletRequest request){
		String recordId = request.getParameter("recordId");
		String returnFee = request.getParameter("deploy_info");

		try{
			TvOrderGoods tog = service.queryTogByRecordId(recordId);
			tog.setState((short) 2);//设置为退货状态

			TvOrder to = orderService.queryObjByOrderId(tog.getOrderId());
			to.setOrderState((short) 60);//更改订单状态
			to.setIsReturngoods((short) 0);//标识.订单有商品退货
			short orderType = to.getIsExperie();

			TvUser tu =jfdhService.queryByUserId(to.getUserId());
			BigDecimal decimal1 = tu.getUserIntegral();//用户积分余额
			BigDecimal decimal2 = new BigDecimal(returnFee);//退货费用
			BigDecimal decimal3 = decimal1.subtract(decimal2);//剩余积分
			if(orderType==2){//积分兑换品
				decimal3 = decimal3.add(to.getOrderAmount());
			}
			tu.setUserIntegral(decimal3);//扣除用户积分抵运费，积分可以为负

			short goodsType = tog.getGoodsType();
			int goodsId = tog.getGoodsId();
			int servicePointId = tog.getServicePointId();
			BigDecimal num = tog.getGoodsNum();
			MallServiceGoods01 msg = new MallServiceGoods01();
			if(goodsType==5||goodsType==7){ //打包品
				msg = jfdhService.queryMsgByMap(goodsId,5,servicePointId);
			}else{
				msg = jfdhService.queryMsgByMap(goodsId,0,servicePointId);
			}
			BigDecimal storage = msg.getGoodsStorage();
			msg.setGoodsStorage(storage.add(num));

			//插入一条积分变动数据
			TvUserIntegralLog til = new TvUserIntegralLog();
			til.setUserId(tu.getUserId());
			til.setOrderId(to.getOrderId());
			til.setIntegralNum(new BigDecimal(returnFee));
			til.setSign((short)0);
			til.setTime(DateUtil.getSystemDateTime());

			service.toReturn(tog,to,tu,msg,til);
			return new AjaxDone("200","退货成功！");
		}catch(Exception e){
			e.printStackTrace();
			return new AjaxDone("300","信息异常,退货失败！");
		}
	}

	/**
	 * export Excel
	 * @param orderSn
	 * @param userName
	 * @param userCardNumber
	 * @param receiverPhone
	 * @param orderState
	 * @param response
	 */
	/*@RequestMapping("export.do")
	public void exportExcel(String orderSn, String userName,String userCardNumber,String receiverPhone, String orderState, HttpServletResponse response) {
		Result rs = null;
		try {
			rs = service.queryOrder( orderSn,  userName, userCardNumber, receiverPhone,  orderState,0,0);
			String[] topName = {"订单编号","会员名称", "会员卡号","会员电话"};
			List<Object[]> list = new ArrayList<Object[]>();
			for(TvOrderGoods orderGoods : (List<TvOrderGoods>)rs.getData()) {
				Object [] obj =new Object[topName.length];
				obj[0]=orderGoods.getOrderSn();
				obj[1]=orderGoods.getUserName();
				obj[2]=orderGoods.getUserCardNumber();
				obj[3]=orderGoods.getReceiverPhone();

				list.add(obj);
			}
			FileExport.exportExcel(topName, list, "可退货订单信息", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
}
